package stock.fish;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class Box {

    List<Fish> fishList = new ArrayList<>();
    int currentWeight = 0;

    public Box(List<Fish> fishList, int currentWeight) {
        this.fishList = new ArrayList<>(fishList);
        this.currentWeight = currentWeight;
    }

    public static String getPrintStr(List<Fish> fishList){
        StringBuilder str = new StringBuilder();
        List<Fish> collect = fishList.stream().sorted(Comparator.comparing(Fish::getId)).collect(Collectors.toList());
        for (Fish fish : collect){
            if (str.length() > 0){
                str.append(",");
            }
            str.append(fish.getPrintStr());
        }
        return str.toString();
    }

    public static Integer getWeightCalculate(List<Fish> fishList){
        return fishList.stream().mapToInt(Fish::getWeight).sum();
    }


    // 检查当前箱子是否符合要求
    public boolean isValid() {
        return  ((fishList.size() >= FishConfig.MIN_FISH_COUNT && fishList.size() < FishConfig.MAX_FISH_COUNT) ||
                (fishList.size() > FishConfig.MIN_FISH_COUNT && fishList.size() <= FishConfig.MAX_FISH_COUNT))
                &&((currentWeight >= FishConfig.MIN_WEIGHT && currentWeight < FishConfig.MAX_WEIGHT) ||
                (currentWeight > FishConfig.MIN_WEIGHT && currentWeight <= FishConfig.MAX_WEIGHT));
    }

    // 获取箱子当前鱼的数量
    public int getFishCount() {
        return fishList.size();
    }

    // 获取箱子的当前重量
    public int getWeight() {
        return getWeightCalculate(fishList);
    }

    // 清空箱子
    public void clear() {
        fishList.clear();
    }

}
